package com.atguigu.gulimall.order.listener;

import com.alipay.api.AlipayApiException;
import com.atguigu.common.to.mq.SeckillOrdetInfoTo;
import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.concurrent.ExecutionException;

@Service
@RabbitListener(queues = "order.seckill.order.queue")
public class SeckillOrderListener {

    @Autowired
    OrderService orderService;

    @RabbitHandler
    public void listener(SeckillOrdetInfoTo seckillEntity, Channel channel, Message message) throws IOException, AlipayApiException {
        System.out.println("收到秒杀成功发送的信息" + seckillEntity+"准备创建订单");
        try {
            orderService.createSeckillOrder(seckillEntity);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            System.out.println("秒杀订单项目,保存成功........");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("秒杀订单项目出现异常........"+e.getMessage());
        }
    }
}
